<%@page import="com.entity.Topic"%>
<%@page import="com.service.impl.TopicServiceImpl"%>
<%@page import="com.service.TopicService"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("utf-8");
	String opt = request.getParameter("opt");
	TopicService topicService = new TopicServiceImpl();
	boolean flag = true;
	
	if ("del".equals(opt)) {
		//删除
		
		String tid = request.getParameter("tid");
		flag = topicService.deleteTopic(Integer.parseInt(tid));
		if (flag) {
			response.sendRedirect("../admin/topic_list.jsp?msg=del_success");
		} else {
			response.sendRedirect("../admin/topic_list.jsp?msg=del_error");
		}
	} else if ("add".equals(opt)) {
		//添加主题
		
		String tname = request.getParameter("tname");

		flag = topicService.addTopic(tname);
		if (flag) {
			response.sendRedirect("../admin/topic_list.jsp?msg=add_success");
		} else {
			response.sendRedirect("../admin/topic_add.jsp?msg=add_error");
		}
	} else if ("upd".equals(opt)) {
		//修改主题
		String tname = request.getParameter("tname");
		String tid = request.getParameter("tid");
		
		flag = topicService.updateTopic(Integer.parseInt(tid), tname);
		if (flag) {
			response.sendRedirect("../admin/topic_list.jsp?msg=upd_success");
		} else {
			response.sendRedirect("topicCtrl.jsp?tid="+tid);
		}
		
	} else {
		//获取一个主题并转发到修改主题页面
		String tid = request.getParameter("tid");
		Topic topic = topicService.getTopic(Integer.parseInt(tid));
		request.setAttribute("topic", topic);
		request.getRequestDispatcher("../admin/topic_upd.jsp").forward(request, response);

	}
%>